﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iodll;

namespace AviewMotion.Core.IO
{
    public class Cylinder
    {
        private readonly IoOutHepler _cylinderOut;
        private readonly IoOutHepler _cylinderBack;

        private readonly IoInHelper _cylinderOutSensor;
        private readonly IoInHelper _cylinderBackSensor;

        private readonly string _cylinderName;

        /// <summary>
        /// 声明一个双控式气缸
        /// </summary>
        /// <param name="cylinderName">气缸名称</param>
        /// <param name="cylinderOut">气缸伸出输出点</param>
        /// <param name="cylinderBack">气缸缩回输出点</param>
        /// <param name="outSensor">气缸伸出感应输入点</param>
        /// <param name="backSensor">气缸缩回感应输入点</param>
        public Cylinder(string cylinderName, IoOutHepler cylinderOut, IoOutHepler cylinderBack, IoInHelper outSensor, IoInHelper backSensor)
        {
            if (cylinderName == null ||
                cylinderOut == null ||
                cylinderBack == null ||
                outSensor == null ||
                backSensor == null)
            {
                throw new ArgumentNullException(null, "输入参数不能为空");
            }
            _cylinderName = cylinderName;
            _cylinderOut = cylinderOut;
            _cylinderBack = cylinderBack;
            _cylinderOutSensor = outSensor;
            _cylinderBackSensor = backSensor;
        }
        /// <summary>
        /// 声明一个单控式气缸
        /// </summary>
        /// <param name="cylinderName">气缸名称</param>
        /// <param name="cylinderOut">气缸伸出输出点</param>
        /// <param name="outSensor">气缸伸出感应输入点</param>
        /// <param name="backSensor">气缸缩回感应输入点</param>
        public Cylinder(string cylinderName, IoOutHepler cylinderOut, IoInHelper outSensor, IoInHelper backSensor)
        {
            if (cylinderName == null ||
               cylinderOut == null ||
               outSensor == null ||
               backSensor == null)
            {
                throw new ArgumentNullException(null, "输入参数不能为空");
            }
            _cylinderName = cylinderName;
            _cylinderOut = cylinderOut;
            _cylinderOutSensor = outSensor;
            _cylinderBackSensor = backSensor;
        }

        /// <summary>
        /// 气缸伸出
        /// </summary>
        public virtual void Out()
        {
            _cylinderBack?.Out_OFF();
            _cylinderOut.Out_ON();
            int timeout = 3000;
            do
            {
                if (_cylinderOutSensor.In())
                    return;
                else
                {
                    System.Threading.Thread.Sleep(100);
                    timeout -= 100;
                }
            } while (timeout > 0);
            throw new Exception($"{_cylinderName}伸出感应超时!!!");
        }
        /// <summary>
        /// 气缸缩回
        /// </summary>
        public virtual void Back()
        {
            _cylinderOut.Out_OFF();
            _cylinderBack?.Out_ON();
            int timeout = 3000;
            do
            {
                if (_cylinderBackSensor.In())
                    return;
                else
                {
                    System.Threading.Thread.Sleep(100);
                    timeout -= 100;
                }
            } while (timeout > 0);
            throw new Exception($"{_cylinderName}缩回感应超时!!!");
        }



        /// <summary>
        /// 气缸伸出
        /// </summary>
        public virtual bool Out(out string errorMessage)
        {
            errorMessage = string.Empty;

            _cylinderBack?.Out_OFF();
            _cylinderOut.Out_ON();
            int timeout = 5000;
            do
            {
                if (_cylinderOutSensor.In())
                    return true;
                else
                {
                    System.Threading.Thread.Sleep(100);
                    timeout -= 100;
                }
            } while (timeout > 0);
            errorMessage = $"{_cylinderName}伸出感应超时!!!";
            return false;
        }
        /// <summary>
        /// 气缸缩回
        /// </summary>
        public virtual bool Back(out string errorMessage)
        {
            errorMessage = string.Empty;

            _cylinderOut.Out_OFF();
            _cylinderBack?.Out_ON();
            int timeout = 5000;
            do
            {
                if (_cylinderBackSensor.In())
                    return true;
                else
                {
                    System.Threading.Thread.Sleep(100);
                    timeout -= 100;
                }
            } while (timeout > 0);
            errorMessage = $"{_cylinderName}缩回感应超时!!!"; 
            return false;
        }
    }
}
